package com.group56.view.userend.video_module;

import com.group56.factory.AppCMFactory;
import com.group56.model.dao.Impl.VideoDAOImpl;
import com.group56.model.entity.Video;
import com.group56.utils.FXMLSrcMenu;
import com.group56.utils.FXMLViewHandler;
import com.group56.view.FrameworkController;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class VideoListController implements Initializable {
    @FXML
    private TextField keyword;
    @FXML
    private VBox V_box;
    @FXML
    private Button search;
    @FXML
    private ComboBox category;
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        Category();
        onClickCategory();
        VideoDAOImpl VD=new VideoDAOImpl();
        ArrayList<Video> videoArrayList=VD.Total();
        for (int i=0;i<videoArrayList.size();i++){
            V_box.getChildren().add(addVideo(videoArrayList.get(i)));
        }
    }

    public Pane addVideo(Video video){
        Pane pane=new Pane();

        Image image=new Image(video.getPicUrl());
        ImageView imageView=new ImageView();
        imageView.setImage(image);
        imageView.setFitHeight(150);
        imageView.setFitWidth(200);
        imageView.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                Platform.runLater(()->{
                    FrameworkController appFramework = null;
                    try {
                        VideoDetailController.videoName=video.getName();
                        appFramework = (FrameworkController) AppCMFactory.getCtrlrMediator().getController(FXMLSrcMenu.UserEnd.APP_FRAMEWORK.getSrc());
                        FXMLViewHandler.putViewInPane(appFramework.getContentPane(),FXMLViewHandler.getView(FXMLSrcMenu.UserEnd.VIDEO_DETAIL.getSrc()));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
            }
        });
        Label label=new Label(video.getDescription());
        label.setLayoutX(320);
        label.setLayoutY(50);
        pane.getChildren().add(label);
        pane.getChildren().add(imageView);
        return pane;
    }
    public void onClickSearch(){

        ArrayList<Video> videoArrayList1=new ArrayList<>();
        ArrayList<Video> videoArrayList2=new ArrayList<>();
        VideoDAOImpl videoDAO=new VideoDAOImpl();
        videoArrayList1=videoDAO.Total();
        for (int i=0;i<videoArrayList1.size();i++){
            if (videoArrayList1.get(i).getName().contains(keyword.getText())){
                videoArrayList2.add(videoArrayList1.get(i));
            }
        }
        System.out.println(videoArrayList2);
        int size=V_box.getChildren().size();
        for (int i=0;i<size;i++){
            V_box.getChildren().remove(V_box.getChildren().size()-1);
        }

        for (int i=0;i<videoArrayList2.size();i++) {
            V_box.getChildren().add(addVideo(videoArrayList2.get(i)));
        }
    }
    public void Category(){
        category.getItems().addAll("Legs training","Chest training","Waist training","Abdomen training","All");
    }
    public void onClickCategory(){
        category.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue observableValue, Object o, Object t1) {
                VideoDAOImpl videoDAO=new VideoDAOImpl();
                ArrayList<Video> arrayList1=videoDAO.Total();
                if (!category.getValue().equals("All")){
                    ArrayList<Video> arrayList2=new ArrayList<>();
                    for (int i=0;i<arrayList1.size();i++){
                        if (arrayList1.get(i).getCategories().equals(category.getValue())){
                            arrayList2.add(arrayList1.get(i));
                        };
                    }
                    int size=V_box.getChildren().size();
                    for (int i=0;i<size;i++){
                        V_box.getChildren().remove(V_box.getChildren().size()-1);
                    }
                    for (int i=0;i<arrayList2.size();i++) {
                        V_box.getChildren().add(addVideo(arrayList2.get(i)));
                    }
                }else {
                    int size=V_box.getChildren().size();
                    for (int i=0;i<size;i++){
                        V_box.getChildren().remove(V_box.getChildren().size()-1);
                    }
                    for (int i=0;i<arrayList1.size();i++){
                        V_box.getChildren().add(addVideo(arrayList1.get(i)));
                    }
                }
            }
        });
//            VideoDAOImpl videoDAO=new VideoDAOImpl();
//            ArrayList<Video> arrayList1=videoDAO.Total();
//            ArrayList<Video> arrayList2=new ArrayList<>();
//            for (int i=0;i<arrayList1.size();i++){
//                if (arrayList1.get(i).getCategories().equals(category.getValue())){
//                    arrayList2.add(arrayList1.get(i));
//                };
//            }
//            int size=V_box.getChildren().size();
//            for (int i=0;i<size;i++){
//                V_box.getChildren().remove(i);
//            }
//            for (int i=0;i<arrayList2.size();i++) {
//                V_box.getChildren().add(addVideo(arrayList2.get(i)));
//            }
        }
}
